<template>
  <div>
    <basic-container>
      <avue-crud :option="option"
                 :table-loading="loading"
                 :data="data"
                 :page.sync="page"
                 :permission="permissionList"
                 :before-open="beforeOpen"
                 v-model="form"
                 ref="crud"
                 @row-update="rowUpdate"
                 @row-save="rowSave"
                 @row-del="rowDel"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 @selection-change="selectionChange"
                 @current-change="currentChange"
                 @size-change="sizeChange"
                 @refresh-change="refreshChange"
                 @on-load="onLoad">
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.question_delete"
                     @click="handleDelete">删 除
          </el-button>
          <el-button type="success"
                     size="small"
                     plain
                     v-if="permission.question_import"
                     @click="handleImport">导 入<i class="el-icon-upload el-icon--right">
          </i></el-button>

          <el-button type="danger"
                     size="small"
                     icon="el-icon-download"
                     plain
                     v-if="permission.question_export"
                     @click="handleExport">导 出
          </el-button>
        </template>
        <template slot="kgForm">
          <div>
<!--          <el-input  type="text" class="tk" v-model="kh"/>-->
            <el-button  type="primary" icon="el-icon-plus" @click="tk()">添加填空</el-button>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="数据导入"
                 append-to-body
                 :visible.sync="excelBox"
                 width="555px">
        <avue-form :option="excelOption" v-model="excelForm" :show-file-list="false" :upload-after="uploadAfter">
          <template slot="excelTemplate">
            <el-button type="primary" @click="handleTemplate">
              点击下载<i class="el-icon-download el-icon--right"></i>
            </el-button>
          </template>
        </avue-form>
      </el-dialog>
      <el-dialog title="填写答案"
                 append-to-body
                 :visible.sync="dananBox"
                 width="15%">
        <div>
          <el-input  type="text" class="tk" v-model="da"/>
          <el-button  type="primary" icon="el-icon-plus" @click="dxda()">确定</el-button>
        </div>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/jypx/eduquestion/question";
  import {mapGetters} from "vuex";
  import {getToken} from "@/util/auth";
  export default {

    data() {
      return {
        kh:'',
        da:'',
        dananBox:false,
        xh:[1,2,3,4,5,6,7,8,9],

        //导入
        excelBox: false,
        excelForm: {},
        excelOption: {
          submitBtn: false,
          emptyBtn: false,
          column: [{
            label: '表格导入',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '表格导入中，请稍等',
            showFileList: false,
            span: 24,
            propsHttp: {
              fileName: 'multipartFile',
              res: 'data'
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: "/api/eduQuestion/question/import",
            headers: {}
          },
            {
              label: '模板下载',
              prop: 'excelTemplate',
              formslot: true,
              span: 24,
            }
          ]
        },


        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          excelBtn: false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "试题科目",
              prop: "courseId",
              align: "center",
              row: false,
              search: true,
              slot: false,
              type: "select",
              dicUrl: "/api/eduCourse/course/getCourseKcmc",
              props: {
                label: "kcmc",
                value: "id",
              },
              rules: [{
                required: true,
                message: "试题科目",
                trigger: "blur",
              }]
            },
            {
              label: "试题类型",
              prop: "stlx",
              type: "select",
              align: "center",
              dataType: "number",
              hide: false,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=stbh",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入试题类型",
                trigger: "blur"
              }]
            },
            {
              label: "试题内容",
              prop: "stnr",
              span: 19,
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "",
              prop: "kg",
              hide: true,
              span: 1,
              formslot: true
            },
            {
              label: "正确答案",
              prop: "zqda",
              align: "center",
              // readonly:true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "A",
              prop: "xxA",
              hide: true,
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "B",
              prop: "xxB",
              hide: true,
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "C",
              prop: "xxC",
              hide: true,
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "D",
              prop: "xxD",
              hide: true,
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "答案详解",
              prop: "daxj",
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "企业id",
              prop: "bladeDeptId",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
            {
              label: "租户id",
              prop: "tenantId",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
            {
              label: "创建人",
              prop: "createUser",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
            {
              label: "创建部门",
              prop: "createDept",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
            {
              label: "修改用户",
              prop: "updateUser",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
            {
              label: "修改时间",
              prop: "updateTime",
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false
            },
          ]
        },
        data: []
      };
    },
    //监听
    watch: {
      // 'form.stnr':{
      //   handler(){
      //     console.log(this.tk().this.kh()+"3333")
      //     console.log(this.kh+"123")
      //   }
      // },
      // kh(){
      //   console.log(this.tk().this.kh()+"3333")
      //   console.log(this.kh+"123")
      // },
      'form.stlx':{
        handler(val){
          const xxA =this.findObject(this.option.column,'xxA')
          const xxB = this.findObject(this.option.column,'xxB')
          const xxC = this.findObject(this.option.column,'xxC')
          const xxD = this.findObject(this.option.column,'xxD')
          const kg = this.findObject(this.option.column,'kg')
          // const stnr = this.findObject(this.option.column,'stnr')
          // const zqda = this.findObject(this.option.column,'zqda')

          if(val===3){
            xxA.display=false
            xxB.display=false
            xxC.display=false
            xxD.display=false
            kg.display=true
            xxA.rules=[{
              required:false,
              message: "请输入",
              trigger:"blur"
            }]
            xxB.rules=[{
              required:false,
              message: "请输入",
              trigger:"blur"
            }]
            xxC.rules=[{
              required:false,
              message: "请输入",
              trigger:"blur"
            }]
            xxD.rules=[{
              required:false,
              message: "请输入",
              trigger:"blur"
            }]
            // stnr.rules=[{
            //   formslot: true,
            //   required:true,
            //   trigger:"blur"
            // }]
            // zqda.rules=[{
            //   required:true,
            //   trigger:"blur"
            // }]
          }else if(val===1){
            xxA.display=true
            xxB.display=true
            xxC.display=true
            xxD.display=true
            kg.display=false
            xxA.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxB.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxC.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxD.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
          }else if(val===2){
            xxA.display=true
            xxB.display=true
            xxC.display=true
            xxD.display=true
            kg.display=false
            xxA.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxB.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxC.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxD.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
          }else if(val===4){
            xxA.display=true
            xxB.display=true
            xxC.display=false
            xxD.display=false
            kg.display=false
            xxA.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
            xxB.rules=[{
              required:true,
              message: "请输入",
              trigger:"blur"
            }]
          }else {
            xxA.display=false
            xxB.display=false
            xxC.display=false
            xxD.display=false
            kg.display=false
          }
        },
        immediate: true,
      }
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.question_add, false),
          viewBtn: this.vaildData(this.permission.question_view, false),
          delBtn: this.vaildData(this.permission.question_delete, false),
          editBtn: this.vaildData(this.permission.question_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
    },
    methods: {
      dxda(loading){
        this.kh=","
        if (this.form.zqda==""){
          this.form.zqda=this.form.zqda+this.da
        }else if (this.form.zqda!=""){
          this.form.zqda=this.form.zqda+this.kh+this.da
        }
        if (this.da==''){
          this.$message.warning("答案不能为空");
          loading();
          return
        }
        this.da=''
        this.dananBox=false
      },
      tk(){
        this.kh="________,"
        this.form.stnr=this.form.stnr+this.kh
        this.dananBox=true
      },

      handleTemplate() {
        window.open(`/api/eduQuestion/question/export-template?${this.website.tokenHeader}=${getToken()}`);
      },
      handleExport() {
        this.$confirm("是否导出试题信息?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          window.open(`/api/eduQuestion/question/export-Question?${this.website.tokenHeader}=${getToken()}`);
        });
      },
      // 弹出导入框
      handleImport() {
        debugger
        this.excelBox = true;
      },

      // 导入前判断
      beforeUpload(file, done, loading) {
        debugger
        this.isFailed = false;
        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        const extension = testmsg === 'xls'
        const extension2 = testmsg === 'xlsx'
        if(!extension && !extension2) {
          this.$message.warning('请上传 .xls,.xlsx 标准格式文件!');
          loading()
        } else {
          done()
        }
      },

      // 导入失败后
      uploadError(error, column) {
        console.log(error,column);
        this.isFailed = true;
      },

      // 导入成功后
      uploadAfter(res, done, loading, column) {
        debugger
        window.console.log(column);
        this.excelBox = false;
        this.refreshChange();
        done();
      },

      // 取消
      cancle() {
        this.page.currentPage = 1;
        this.onLoad(this.page);
      },
      rowSave(row, done, loading) {
        if (row.stlx===1) {
          if (row.zqda.length > 1) {
            this.$message.warning("试题类型为单选");
            loading();
            return
          }
        }
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        if (row.stlx===1) {
          if (row.zqda.length > 1) {
          this.$message.warning("试题类型为单选");
          loading();
          return
        }
        }
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style lang="scss" scpoed>

  .tk{
    width: 150px;
  }
.sttp_content {
  width: 100%;
  height: 118px;
  line-height: 105px;
  border: 1px solid #DCDFE6;
  border-radius: 4px;

  .el-tag {
    height: 100px;
    line-height: 100px;
    padding: 0px 10px 0px 0px;
    margin-left: 10px;
    margin-top: 10px;

    .el-icon-close {
      top: -84px;
    }
  }

  img {
    width: 98px;
    height: 98px;
    cursor: pointer;
  }
}

.ope_row_div {
  .el-button {
    font-size: 12px;
  }
}
</style>
